<?php

namespace App\Http\Controllers\EmberApi;
use App\Pool\DataReader;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Pool\Statistics\Parser as StatisticsParser;
use App\Pool\Miners\Parser as MinersParser;
use Carbon\Carbon;
use App\Miners\Miner;
use Illuminate\Support\Facades\Redis;



class StatsController extends Controller
{
    //
    public function index(){
        $cache = Redis::get('xdagpoolstats');
        $stats = json_encode([]);
        if(!isset($cache)){
            $stats = $this->getData();
            Redis::setex('xdagpoolstats',60,$stats);
            $stats = json_decode($stats);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);
    }
    protected function getData(){
        $dataReader = new DataReader();
        $stats = new StatisticsParser($dataReader->getLiveDataJson());
        $miners = new MinersParser($dataReader->getFastDataJson());
        $minersData = $dataReader->getFastDataJson();
        $minersArray = $minersData['miners']??[];
        $activeMiners = [];
        $poolHashrate = 0;
        foreach ($minersArray as $miner) {
            if ($miner['address'] == 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'){
                continue;
            }
            if (!in_array($miner['address'],$activeMiners) & $miner['status'] == 'active'){
                $activeMiners[] = $miner['address'];
            }
        }
        $nodesArray = [];
        $nodesArray[0]['difficulty'] = 0;
        //$nodesArray[0]['difficulty'] = "63412848279";
        $nodesArray[0]['height'] = $stats->getNumberOfBlocks();
        $nodesArray[0]['lastBeat'] = "0";
        $nodesArray[0]['name'] = "main";

        $latestFoundblock = DB::table('found_blocks')
            ->latest('found_at')
            ->first();
        //查询矿池今日爆块数量
        $today = Carbon::today()->toDateString();
        $foundBlocksToday = 0;
        $foundBlocksTodayCount = DB::table('found_blocks')
            ->whereDate('found_at',$today)
            ->count();
        if (isset($foundBlocksTodayCount)){
            $foundBlocksToday = $foundBlocksTodayCount;
        }
        //查询昨日爆块数量
        $yesterday = Carbon::yesterday()->toDateString();
        $foundBlocksYesterday = 0;
        $foundBlocksYesterdayCount = DB::table('found_blocks')
            ->whereDate('found_at',$yesterday)
            ->count();
        if (isset($foundBlocksYesterdayCount)){
            $foundBlocksYesterday = $foundBlocksYesterdayCount;
        }
        //查询矿池总爆块数量
        $foundBlocksTotal = 0;
        $foundBlocksTotalCount = DB::table('found_blocks')
            ->count();
        if (isset($foundBlocksTotalCount)){
            $foundBlocksTotal = $foundBlocksTotalCount;
        }
        $statsArray = [];
        $statsArray['foundBlocksToday'] = $foundBlocksToday;
        $statsArray['foundBlocksTotal'] = $foundBlocksTotal;
        $statsArray['foundBlocksYesterday'] = $foundBlocksYesterday;

        if(isset($latestFoundblock)){
            $blockFoundTime = $latestFoundblock->found_at;
            $statsArray['lastBlockFound'] = strtotime($blockFoundTime);
        }else{
            $statsArray['lastBlockFound'] = 0;
        }

        $statsArray['roundShares'] = $miners->getTotalUnpaidShares();

        /*$countPoolHashrate = Miner::where('status','active')
            ->sum('hashrate');
        if (isset($countPoolHashrate)){
            $poolHashrate = (float)$countPoolHashrate;
        }
        */
        $jsonArray = [];
        $jsonArray['candidatesTotal'] = 0;
        $jsonArray['difficulty'] = $stats->getDifficulty();
        $jsonArray['minersWorkersTotal'] = $miners->getNumberOfActiveMiners();
        /*
        if ($poolHashrate <> 0){
            $jsonArray['hashrate'] = $poolHashrate;
        }else{
            $jsonArray['hashrate'] = $stats->getPoolHashrate();
        }*/
        $jsonArray['hashrate'] = $stats->getPoolHashrate();
        $jsonArray['networkHashrate'] = $stats->getNetworkHashrate();
        $jsonArray['mainblockHeight'] = $stats->getNumberOfMainBlocks();
        $jsonArray['maturedTotal'] = 0;
        $jsonArray['minersTotal'] = count($activeMiners);
        $jsonArray['nodes'] = $nodesArray;
        $jsonArray['now'] = strtotime("now");
        $jsonArray['stats'] = $statsArray;
        //var_dump($jsonArray);
        return json_encode($jsonArray);
    }

}
